﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using PPI.Client.Entities;
using PPI.Client.Utils;
using PPI.Controls;
using PPIReporting;

namespace PPI
{
    public partial class frmReports : frmBase
    {
        public frmReports()
        {
            InitializeComponent();
            base.MenuMain.Visible = false;
            this.FormSize = FormSizes.Large;
            this.MaximizeBox = true;
        }

        private void frmReports_Load(object sender, EventArgs e)
        {
            this.chooseReport1.LoadCombo();
        }

        private void chooseReport1_LoadReport(object sender, EventArgs e)
        {
            try
            {
                Assembly _ReportAssembly = Assembly.ReflectionOnlyLoadFrom("PPIReporting.dll");

                reportViewer1.LocalReport.AddTrustedCodeModuleInCurrentAppDomain("PPIReporting.dll");
                reportViewer1.Reset();
                reportViewer1.ProcessingMode = ProcessingMode.Local;
                reportViewer1.LocalReport.DataSources.Clear();
                
                if(chooseReport1.SelectedReport.HasManyDatasources)
                {
                    ReportDataSource datasourceReport = new ReportDataSource(chooseReport1.SelectedReport.Datasource, 
                        chooseReport1.DataSource.Tables[chooseReport1.SelectedReport.Datasource]);
                    reportViewer1.LocalReport.DataSources.Add(datasourceReport);
                    foreach (string s in chooseReport1.SelectedReport.OtherDatasources)
                    {
                        datasourceReport = new ReportDataSource(s, chooseReport1.DataSource.Tables[s]);
                        reportViewer1.LocalReport.DataSources.Add(datasourceReport);
                    }
                }else
                {
                    ReportDataSource datasource = new ReportDataSource(chooseReport1.SelectedReport.Datasource, 
                        chooseReport1.DataSource.Tables[0]);
                    reportViewer1.LocalReport.DataSources.Add(datasource);    

                }

                reportViewer1.LocalReport.LoadReportDefinition(_ReportAssembly.GetManifestResourceStream(chooseReport1.SelectedReport.FileName));
                this.reportViewer1.RefreshReport();
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void chooseReport1_SizePanelFilterChanged(object sender, ChooseReportEventArgs e)
        {
            ChooseReportEventArgs sizeEvents = (ChooseReportEventArgs)e;
            chooseReport1.Height = sizeEvents.NewHeigth;

        }
    }
}
